package com.floreantpos.ui.ticket;

import java.awt.Color;
import java.awt.Component;
import java.text.DecimalFormat;

import javax.swing.JTable;
import javax.swing.SwingConstants;
import javax.swing.table.DefaultTableCellRenderer;

import com.floreantpos.model.TicketItem;
import com.floreantpos.model.TicketItemModifier;
import com.floreantpos.model.TicketItemModifierGroup;
import com.floreantpos.ui.views.order.OrderView;
import java.text.DecimalFormatSymbols;
import java.util.List;
import java.util.Locale;

public class TicketTableCellRenderer extends DefaultTableCellRenderer {

  private static final DecimalFormat numberFormat = new DecimalFormat("#,##0.00", new DecimalFormatSymbols(new Locale("pt", "BR")));
  private boolean inTicketScreen = false;
  private int lastItemRow = 0;
  private boolean lastRowUpdated = false;

  @Override
  public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
    Component rendererComponent = super.getTableCellRendererComponent(table, value, isSelected, false, row, column);

    TicketTableModel model = (TicketTableModel) table.getModel();
    Object object = model.get(row);

    if (!inTicketScreen || isSelected) {
      if (lastRowUpdated && row != lastItemRow && object instanceof TicketItem) {
        lastItemRow = 0;
        lastRowUpdated = false;
      }

      if (!lastRowUpdated && lastItemRow == 0 && OrderView.getInstance().getModifierView().isShowing()) {
        lastItemRow = model.tableRows.size() - 1;
        lastRowUpdated = true;
      }

      if (lastRowUpdated && row < lastItemRow && OrderView.getInstance().getModifierView().isShowing()) {
        lastItemRow = 0;
        lastRowUpdated = false;
        OrderView.getInstance().showView("ITEM_VIEW");
      }

      return rendererComponent;
    }

    rendererComponent.setBackground(Color.WHITE);

    if (object instanceof TicketItem) {
      Boolean isMissingModifier = false;
      TicketItem ticketItem = (TicketItem) object;

      List<TicketItemModifierGroup> modifierGroups = ticketItem.getTicketItemModifierGroups();
      if (modifierGroups != null && !modifierGroups.isEmpty()) {
        for (TicketItemModifierGroup modifierGroup : modifierGroups) {
          List<TicketItemModifier> modifiers = modifierGroup.getTicketItemModifiers();
          if (modifierGroup.getMinQuantity() > 0) {
            if (!modifiers.isEmpty()) {
              for (TicketItemModifier modifier : modifiers) {
                if (modifier.getItemCount() < modifier.getMinQuantity()) {
                  isMissingModifier = true;
                } else if (modifier.isPizzaModifier() && modifierGroup.countItems(true) > modifierGroup.getMaxQuantity()) {
                  isMissingModifier = true;
                } else {
                  isMissingModifier = false;
                }
              }
            } else if (ticketItem.isModifierMandatory()) {
              isMissingModifier = true;
            }
          } else if (ticketItem.isModifierMandatory()) {
            isMissingModifier = true;
          }
        }
      } else if (ticketItem.isModifierMandatory()) {
        isMissingModifier = true;
      }

      if (ticketItem.isPrintedToKitchen()) {
        rendererComponent.setBackground(Color.YELLOW);
      } else if (isMissingModifier) {
        rendererComponent.setBackground(Color.ORANGE);
      }
    }
    if (object instanceof TicketItemModifier) {
      TicketItemModifier modifier = (TicketItemModifier) object;
      if (modifier.isPrintedToKitchen()) {
        rendererComponent.setBackground(Color.YELLOW);
      } else if (modifier.getItemCount() < modifier.getMinQuantity()) {
        rendererComponent.setBackground(Color.ORANGE);
      }
    }

    return rendererComponent;
  }

  @Override
  protected void setValue(Object value) {
    if (value == null) {
      setText("");
      return;
    }

    String text = value.toString();

    if (value instanceof Double || value instanceof Float) {
      text = numberFormat.format(((java.lang.Number) value).doubleValue());
      setHorizontalAlignment(SwingConstants.RIGHT);
    } else if (value instanceof Integer) {
      setHorizontalAlignment(SwingConstants.RIGHT);
    } else {
      setHorizontalAlignment(SwingConstants.LEFT);
    }

    setText(" " + text + " ");
  }

  public boolean isInTicketScreen() {
    return inTicketScreen;
  }

  public void setInTicketScreen(boolean inTicketScreen) {
    this.inTicketScreen = inTicketScreen;
  }
}
